package com.android.lovepick;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.android.lovepick.http.HttpThread;
import com.android.lovepick.other.GetNewMsgService;
import com.android.lovepick.util.Constant;
import com.android.lovepick.util.DialogHelper;

public class TestActivity extends Activity
{
	private final String TEST_TAG = "TestActivity";
	private ProgressDialog mDialog = null;

	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.test);
		mDialog = new ProgressDialog(this);
	}

	public void showDialogFrag(int dialogId, String title, String msg,
			DialogInterface.OnClickListener listener)
	{
		DialogFragment dialogFragment = DialogHelper.newInstance(dialogId,
				title, msg, listener);
		dialogFragment.show(getFragmentManager(), "dialog");
	}

	public void doTest(View view)
	{
		HttpThread thread = new HttpThread(mHandler, Constant.HTTP_METHOD_GET,
				Constant.HTTP_TEST, null);
		thread.start();
		mDialog.show();
	}

	public void toMapView(View v)
	{
		MainApp app = (MainApp) this.getApplication();
		if (app.bmapMan != null)
		{
			app.bmapMan.destroy();
			app.bmapMan = null;
		}
		Intent intent = new Intent(TestActivity.this, MapViewActivity.class);
		startActivity(intent);
	}

	public void toMyOrder(View v)
	{
		String src = "西单";
		double slat = 39.915098;
		double slng = 116.377284;
		String dest = "北京首都国际机场";
		double dlat = 40.088197;
		double dlng = 116.603378;
		int iDriver = 0;
		String offset = "10";
		String money = "10";
		String goTime = "2012-09-26 17:29:03";

		Log.i(TEST_TAG, "save my current order infos");

		SharedPreferences myCurrentOrderPrefs = getSharedPreferences(
				Constant.PREFS_MY_CURRENT_ORDER, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = myCurrentOrderPrefs.edit();
		editor.putBoolean("isRecommend", true);
		editor.putString("src", src);
		editor.putString("slat", String.valueOf(slat));
		editor.putString("slng", String.valueOf(slng));
		editor.putString("dest", dest);
		editor.putString("dlat", String.valueOf(dlat));
		editor.putString("dlng", String.valueOf(dlng));
		editor.putInt("iDriver", iDriver);
		editor.putString("offset", offset);
		editor.putString("goTime", goTime);

		editor.putString("money", money);
		editor.commit();

		String jsonString = "["
				+ "{'orderID': '40', 'nickName': 'zp1987123', 'goTime': '2012-09-26 17:29:03', 'src': '北邮', 'slat': '39.967433', 'slng': '116.363737', 'dest': '北科', 'dlat': '39.995795', 'dlng': '116.36', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}, "
				+ "{'orderID': '41', 'nickName': 'zp1987123', 'goTime': '2012-09-26 19:50:49', 'src': '西单', 'slat': '39.915098', 'slng': '116.377284', 'dest': '北京首都国际机场', 'dlat': '40.088197', 'dlng': '116.603378', 'money': '10', 'state': '待处理'}"
				+ "]";
		// Log.i(TEST_TAG, jsonString);
		Intent intent = new Intent(TestActivity.this, MyOrderActivity.class);
		intent.putExtra("myorder", jsonString);
		intent.putExtra("iDriver", 0);
		intent.putExtra("launch_or_recommend", true);
		startActivity(intent);
	}

	public void toTestPath(View v)
	{
		Intent intent = new Intent(TestActivity.this, TestPathActivity.class);
		startActivity(intent);
	}

	public void toTestMsg(View v)
	{
		Intent intent = new Intent(TestActivity.this, TestMsgActivity.class);
		startActivity(intent);
	}

	public void toTestMsgNew(View v)
	{
		Intent service = new Intent(TestActivity.this, GetNewMsgService.class);
		service.putExtra("LastID", 0);
		startService(service);
		Log.i(TEST_TAG, "start service");
	}

	public void stopGetNewMsg(View v)
	{
		GetNewMsgService service = new GetNewMsgService();
		if (service.isRunning())
		{
			Log.i(TEST_TAG, "停止消息接收服务!");
			service.setRunning(false);
		}
	}

	@Override
	protected void onDestroy()
	{
		MainApp app = (MainApp) this.getApplication();
		if (app.bmapMan != null)
		{
			app.bmapMan.destroy();
			app.bmapMan = null;
		}
		super.onDestroy();
		Log.i(TEST_TAG, "bmapMan destroyed.");
	}

	private Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case Constant.MSG_HTTP_SUCCESS:
					String result = msg.getData().getString("http_result");
					Log.i(TEST_TAG, result);
					mDialog.dismiss();
					break;

				case Constant.MSG_HTTP_ERROR:
					Log.i(TEST_TAG, "http error...");
					mDialog.dismiss();
					break;

				default:
					break;
			}
		}
	};
}
